Shell脚本实战15-开发Rsync服务启动脚本

1. 需求

写出网络服务独立进程模式下Rsync的系统启动脚本。例如:/etc/init.d/rsyncd {start|stop|restart}。

要求:

  • 要使用系统函数库技巧
  • 要用函数,不能将一堆代码混在一起
  • 可悲chkconfig管理

2. 过程

2.1. 实现过程

2.1.1. 第一步

rsync服务的简单准备,代码如下:

1
2
3
4
5
6
7
# rpm -qa rsync #<==查看软件包是否安装
# yum install -y rsync #<==如果没有安装rsync,则安装,否则就跳过这一步操作
# touch /etc/rsyncd.conf #<==为了简便起见,这里创建了一个空文件,实际工作中是有配置内容的。
# rsync --daemon #<==启动rsync服务命令
# netstat -lnt | grep 873 #<==过滤873端口,即rsync端口服务
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN
tcp 0 0 :::873 :::* LISTEN

2.1.2. 第二步

实现rsync服务的启动和停止方法,以及端口检测方法。

停止方法:

1
2
# pkill rsync
# netstat -lntup | grep 873

启动方法:

1
2
3
4
# rsync --daemon
# netstat -lnt | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN
tcp 0 0 :::873 :::* LISTEN

2.1.3. 第三步

常规方法有检测端口以及进程是否存在,还可以当服务启动时,创建一个锁文件(/var/lock/subsys/),而当服务停止时,就删除这个锁文件,这样就可以通过判断这个文件的有无,来确定服务是否是启动状态,这是一些系统脚本常用的手法。

2.1.4. 第四步

开发rsync服务的启动脚本/etc/init.d/rsyncd,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
if [ $# -ne 1 ]
then
echo $"usage:$0 {start|stop|restart}"
exit 1
fi
if [ "$1" = "start" ]
then
rsync --daemon
sleep 2 #<==休息2秒,这点很重要,停止及启动服务后,建议先休息1-2秒,再判断
if [ `netstat -lntup | grep rsync | wc -l` -ge 1 ]
then
echo "rsync is started."
exit 0
fi
elif [ "$1" = "stop" ]
then
killall rsync &> /dev/null
sleep 2
if [ `netstat -lntup | grep rsync | wc -l` -eq 0 ]
then
echo "rsyncd is stopped."
exit 0
fi
elif [ "$1" = "restart" ]
then
killall rsync
sleep 1
killpro=`netstat -lntup | grep rsync | wc -l`
rsync --daemon
sleep 1
startpro=`netstat -lntup | grep rsync | wc -l`
if [ $killpro -eq 0 -a $startpro -eq 1 ]
then
echo "rsyncd is restarted."
exit 0
fi
else
echo $"usage:$0 {start|stop|restart}"
exit 1
fi

此脚本可以实现基本的启动和停止功能,但是,离专业和规范还有一定的距离。比如更专业的可以用case语句实现。

2.2. 实现chkconfig管理

2.2.1. 实现说明

chkconfig命令是系统用来管理脚本开机自启动的命令,但是需要脚本支持chkconfig管理才行,具体的方法是在脚本的开头解释器之后加入如下两行内容:

1
2
# chkconfig: 2345 20 80
# description: Save and restores system entropy pool

说明:

  • 两行内容的开头都要有#
  • 第一行是说需要chkconfig管理,2345是Linux运行级别,表示该脚本默认在2345级别为启动状态,20是脚本的开始启动顺序,80是脚本的停止顺序,这两个数字都是不超过99的数字。一般情况下,可以根据服务的启动需求来选择,应用服务一般要靠后启动为准,越早停止越好。

2.2.2. 最终的脚本

最终的rsync的启动脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
# chkconfig: 2345 20 80
# description: Save and restores system entropy pool
if [ $# -ne 1 ]
then
echo $"usage:$0 {start|stop|restart}"
exit 1
fi
if [ "$1" = "start" ]
then
rsync --daemon
sleep 2 #<==休息2秒,这点很重要,停止及启动服务后,建议先休息1-2秒,再判断
if [ `netstat -lntup | grep rsync | wc -l` -ge 1 ]
then
echo "rsync is started."
exit 0
fi
elif [ "$1" = "stop" ]
then
killall rsync &> /dev/null
sleep 2
if [ `netstat -lntup | grep rsync | wc -l` -eq 0 ]
then
echo "rsyncd is stopped."
exit 0
fi
elif [ "$1" = "restart" ]
then
killall rsync
sleep 1
killpro=`netstat -lntup | grep rsync | wc -l`
rsync --daemon
sleep 1
startpro=`netstat -lntup | grep rsync | wc -l`
if [ $killpro -eq 0 -a $startpro -eq 1 ]
then
echo "rsyncd is restarted."
exit 0
fi
else
echo $"usage:$0 {start|stop|restart}"
exit 1
fi

2.3. 最后的步骤

设置开机自启动的过程如下:

  • 设置前检查rsyncd的开机自启动情况

    1
    # chkconfig --list rsyncd #<==因为没有设置,所以没有输出
  • 添加脚本开机自启动并检查:

    1
    2
    3
    # chkconfig --add rsyncd #<==注意,rsyncd脚本一定要放在/etc/init.d/目录下
    # chkconfig list rsyncd #<==检查自启动结果,2345级别默认为启动状态
    rsyncd 0: 关闭 1: 关闭 2: 启用 3: 启用 4: 启用 5: 启用 6: 关闭
0%